package com.ueboot.weishop.api.controller.customer;

import com.ueboot.core.exception.BusinessException;
import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.api.SessionUtils;
import com.ueboot.weishop.api.controller.customer.vo.CustomerInvoiceReq;
import com.ueboot.weishop.api.controller.customer.vo.CustomerInvoiceResp;
import com.ueboot.weishop.entity.customer.CustomerInfoEntity;
import com.ueboot.weishop.entity.customer.CustomerInvoiceEntity;
import com.ueboot.weishop.service.customer.CustomerInvoiceService;
import com.ueboot.weishop.service.customer.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zl  on 2017-11-05 10:01:14.
 * 我的信息接口
 * - 新增发票信息接口
 * - 查询发票信息接口
 * - 修收发票信息接口
 * - 删除发票信息接口
 * - 设置默认发票接口
 *
 * @author zl
 * @since 2.0 by autocode
 */

@Slf4j
@RestController
@RequestMapping(value = "/api/private/customer/invoices")
public class CustomerInvoiceController {

    @Autowired
    private CustomerInvoiceService service;

    @Autowired
    private CustomerService customerService;


    @RequestMapping(value = "/lists", method = RequestMethod.GET)
    public Response<List<CustomerInvoiceResp>> list() {
        String openId = SessionUtils.getOpenId();
        if (StringUtils.isEmpty(openId)) {
            throw new BusinessException("请使用微信！");
        }
        CustomerInfoEntity customerInfoEntity = customerService.findByOpenId(SessionUtils.getOpenId());
        if (customerInfoEntity == null) {
            throw new BusinessException("会员信息不存在");
        }
        List<CustomerInvoiceEntity> entities = service.findByCustomerId(customerInfoEntity.getId());
        List<CustomerInvoiceResp> body = new ArrayList<>();
      for (CustomerInvoiceEntity entity : entities) {
           CustomerInvoiceResp resp = new CustomerInvoiceResp();
           BeanUtils.copyProperties(entity, resp);
           body.add(resp);
       }
        return new Response<>(body);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody CustomerInvoiceReq req) {
        CustomerInfoEntity customerInfoEntity = customerService.findByOpenId(SessionUtils.getOpenId());
        CustomerInvoiceEntity entity = null;
        if (req.getId() == null) {
            entity = new CustomerInvoiceEntity();
        } else {
            entity = service.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);
        entity.setCustomer(customerInfoEntity);
        if(req.getDefaultInvoice() == null){
            entity.setDefaultInvoice(false);
        }else {
            entity.setDefaultInvoice(req.getDefaultInvoice());
        }
        service.save(entity);
        return new Response<>();
    }


    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Response<Void> delete(@PathVariable Long id) {
        service.delete(id);
        return new Response<>();
    }

    @RequestMapping(value = "/is/default", method = RequestMethod.POST)
    public Response<Void> isDefault(@RequestBody CustomerInvoiceReq req) {
        CustomerInfoEntity customerInfoEntity = customerService.findByOpenId(SessionUtils.getOpenId());
        service.updateUnDefaultByCustomerId(customerInfoEntity.getId(), Boolean.FALSE);
        CustomerInvoiceEntity entity = service.get(req.getId());
        entity.setDefaultInvoice(true);
        service.save(entity);
        return new Response<>();
    }




}
